package com.pesilo.module.watch.mvc.view.component
{
	import com.greensock.TweenMax;
	import com.pesilo.base.BaseComponent;
	import com.pesilo.module.watch.event.WatchEvent;
	import com.pesilo.module.watch.ui.BgLine;
	import com.pesilo.module.watch.ui.ProgressLine;
	
	import flash.display.Sprite;
	import flash.display3D.IndexBuffer3D;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class RulerComponent extends BaseComponent
	{
		private static const PD:Number = 0.18;
		
		private var rulerMc:Sprite;
		
		private var lineMc:Sprite;
		private var bg:BgLine;
		private var lpLine:ProgressLine;
		private var rpLine:ProgressLine;
		
		private var rulerNum:int=15;//尺子上的个数
		private var rulerLength:Number;//尺子的长度
		private var itemWidth:Number=70;//Item标准宽度
		private var scaleVal:Number;//缩放比例
		private var showNum:int=5;
		
		public var rulerArr:Array=[];
		
		
		public function RulerComponent()
		{
			super();
		}
		
		override public function initUI():void{
			rulerMc=new Sprite();
			this.addChild(rulerMc);
			
			lineMc=new Sprite();
			this.addChild(lineMc);
			bg=new BgLine();
			lineMc.addChild(bg);
			lpLine=new ProgressLine();
			lineMc.addChild(lpLine);
			rpLine=new ProgressLine();
			lineMc.addChild(rpLine);
			lpLine.width=rpLine.width=0;
		}
		
		public function loadingProgressInfo(p:Number,len:int):void{
			bg.width=50*len;
			rulerLength=50*len;
			if(len==1){
				bg.width=70;
				rulerLength=70;
			}
			
			lpLine.x=rpLine.x=bg.width/2;
			
			lpLine.width=rpLine.width=bg.width/2*p;
			lpLine.x=bg.width/2-bg.width/2*p;
		}
		
		public function createRulerList(arr:Array):void
		{		
			rulerArr=[];
			rulerNum=arr.length;
			showNum=5;
			if(rulerNum<=7&&rulerNum>=5)showNum=3;
			if(rulerNum<5)showNum=1;
			
			var itemLength:Number=rulerLength/rulerNum;//缩放后的宽度
			for(var i:int=0;i<arr.length;i++){
				var rulerItemCmp:RulerItemComponent=new RulerItemComponent(i,arr[i]);
				rulerMc.addChild(rulerItemCmp);
				itemWidth=rulerItemCmp.width;
				rulerArr.push(rulerItemCmp);
				scaleVal=(itemLength*100)/itemWidth/100;
				if (itemLength < itemWidth) {
					rulerItemCmp.scaleX = rulerItemCmp.scaleY = scaleVal;
				}
				rulerItemCmp.x=(rulerItemCmp.width-PD)*i;
				
				rulerItemCmp.addEventListener(WatchEvent.ITEM_CLICK_EVENT,__itemClickHandle);
				rulerItemCmp.addEventListener(WatchEvent.ITEM_ROLL_OVER_EVENT,__itemRollEventHandle);
				rulerItemCmp.addEventListener(WatchEvent.ITEM_ROLL_OUT_EVENT,__itemRollEventHandle);
			}
			rulerArr[0].showSign(true);
		}
		
		protected function __itemClickHandle(e:WatchEvent):void
		{
			dispatchEvent(new WatchEvent(WatchEvent.ITEM_CLICK_EVENT,int(e.data)));
		}
		
		protected function __itemRollEventHandle(e:WatchEvent):void
		{
			switch(e.type){
				case WatchEvent.ITEM_ROLL_OVER_EVENT:
					itemOverEff(int(e.data));
					break;
				case WatchEvent.ITEM_ROLL_OUT_EVENT:
					itemOutEff();
					break;
			}
		}
		
		public function rollOverEff():void{
			for(var i:int=-2;i<rulerNum+3;i++){
				TweenMax.delayedCall(0.15*i,itemOverEff,[i]);
			}
		}
		
		/**
		 * item缩入效果
		 */
		public function itemOutEff():void{
			var itemLength:Number = rulerLength / rulerNum;//缩放后的宽度
			var itemScale:Number=(itemLength * 100) / itemWidth / 100;
			var wayOut:Number = 0;
			for (var o:int = 0; o < rulerNum; o++ ) {
				TweenMax.to(rulerArr[o], 0.5, { x:wayOut } );
				wayOut += itemLength;
				TweenMax.to(rulerArr[o], 0.5, { scaleX:itemScale, scaleY:itemScale } );
				rulerArr[o].roll(0);
			}
		}
		
		/**
		 * @index 当前目标的索引
		 * item 滑出效果
		 */
		public function itemOverEff(index:int):void{
//			var index:int=int(e.data);
			var wayArr:Array=new Array();
			var way:Number=0;
			var touch:int=0;
			
			itemOutEff();
			//调用roll函数
			for(var k:int=0;k<showNum;k++){
				if(rulerArr[index- Math.floor(showNum/2) +k]){
					rulerArr[index -  Math.floor(showNum/2)  + k].roll(Math.abs(Math.abs( Math.floor(showNum/2)  - k) - 3));
				}
			}
			
			var overLength:Number=rulerLength;
			for(var i:int=0;i<showNum;i++){
				if (rulerArr[index - Math.floor(showNum/2) + i]) {//如果存在
					var sn:Number = 1-Math.abs(i -  Math.floor(showNum/2))*PD;
					TweenMax.to(rulerArr[index -  Math.floor(showNum/2)  + i], 0.5, { scaleX:sn, scaleY:sn } );
					touch++;
					overLength -= itemWidth * sn;
				}
			}
			
			overLength /= rulerNum-touch;
			var overScale:Number = overLength*100 / itemWidth/100;
			
			for(var j:int=0;j<rulerNum;j++){
				if (j >= index -  Math.floor(showNum/2) && j <= index +  Math.floor(showNum/2)) {
					if (index == j) {
						wayArr[j]=itemWidth;
					}else if (Math.abs(index - j) == 1) {
						wayArr[j]=itemWidth*(1-PD);
					}else if (Math.abs(index - j) == 2) {
						wayArr[j]=itemWidth*(1-PD*2);
					}
				}else {
					wayArr[j] = overLength;
					TweenMax.to(rulerArr[j], 0.5, { scaleX:overScale, scaleY:overScale } );
				}
			}
			
			var myWay:Number = 0;
			for (var l:int = 0; l < rulerNum; l++ ) {
				TweenMax.to(rulerArr[l], 0.5, { x:myWay } );
				myWay += wayArr[l];
			}
		}
		
		override public function initEvent(b:Boolean=true):void{
			if(b){
			}else{
			}
		}
		
				
		
		
		override public function initLocation():void{

		}
		
		
	}
}